<template>
  <div>
    <v-top></v-top>
    <van-address-edit
      :address-info="detail"
      :area-list="areaList"
      show-set-default
      :area-columns-placeholder="['请选择', '请选择', '请选择']"
      @save="onSave"
    >
      <div class="line van-cell van-field">
        <span class="van-cell__title van-field__label">标签</span>
        <van-radio-group v-model="tag" class="van-cell__value van-field__value">
          <van-radio name="家">家</van-radio>
          <van-radio name="公司">公司</van-radio>
          <van-radio name="学校">学校</van-radio>
        </van-radio-group>
      </div>
    </van-address-edit>
  </div>
</template>

<script>
import { areaList } from "@vant/area-data";
import { reqAddressAdd, reqAddressEdit, reqAddressList } from "../../http/api";
import { Toast } from "vant";
export default {
  data() {
    return {
      // 省市区的数据
      areaList,
      tag: "家",
      detail: {},
    };
  },
  mounted() {
    // 4.如果编辑进来了
    if (this.$route.query.id) {
      //此处是编辑进来的
      reqAddressList({
        uid: JSON.parse(localStorage.getItem("user")).uid,
        id: this.$route.query.id,
      }).then((res) => {
        let detail = res.data.list[0];

        this.detail = {
          name: detail.username,
          tel: detail.userphone,
          address: detail.location,
          areaCode: detail.regioncode,
          addressDetail: detail.useraddress,
          isDefault: detail.status == 1,
        };
        this.tag = detail.label;
      });
    }
  },
  methods: {
    onSave(e) {
      if (this.$route.query.id) {
        //修改
        reqAddressEdit({
          username: e.name,
          userphone: e.tel,
          regioncode: e.areaCode,
          location: e.province + e.city + e.county,
          useraddress: e.addressDetail,
          status: e.isDefault ? 1 : 0,
          label: this.tag,
          id: this.$route.query.id,
        }).then((res) => {
          if (res.data.code == 200) {
            //弹成功
            Toast(res.data.msg);
            //跳转到list
            this.$router.replace("/addressAdd");
          }
        });
      } else {
        //添加
        //3.添加
        reqAddressAdd({
          uid: JSON.parse(localStorage.getItem("user")).uid,
          username: e.name,
          userphone: e.tel,
          regioncode: e.areaCode,
          location: e.province + e.city + e.county,
          useraddress: e.addressDetail,
          status: e.isDefault ? 1 : 0,
          label: this.tag,
        }).then((res) => {
          if (res.data.code == 200) {
            //弹成功
            Toast(res.data.msg);
            //跳转到list
            this.$router.replace("/a-list");
          }
        });
      }
    },
  },
};
</script>

<style scoped>
.van-radio-group {
  overflow: hidden;
}
.van-radio {
  float: left;
}
</style>